https://bntr.livejournal.com/26760.html

текст


<?php

$file = $_SERVER['QUERY_STRING'];
if( empty($file) )  die("Use: ?filename");
$fp = @fopen( $file, "r" ) or die("Can't open: $file");

print "<pre>";

// Spans

$spanKeyword = array( '<font color="#800080">', '</font>' );
$spanComment = array( '<font color="#FF8000">', '</font>' );

// Preg patterns

$keywords = 'and|or|xor|exception|as|break|case|class|const|continue|'.
            'declare|default|die|do|else|empty|for|foreach|function|'.
            'global|if|new|print|static|switch|use|var|while';
$comments = array('\/\/','\/\*','\*\/');    //  '//','/*','*/' quoted with slashes

$pattern[0] = "/(.*)(\b($keywords)\b|({$comments[0]})|({$comments[1]}))(.*)$/U";
$pattern[1] = "/((.*){$comments[2]})(.*)$/U";
$commentMode = 0;


function printHtml( $str )  {  print htmlspecialchars( $str ); }


while( !feof($fp) )
{
    $str = fgets( $fp, 0x400 );
    
    while( $str )
        if( !preg_match( $pattern[$commentMode], $str, $matches ) )  {
            printHtml( $str );
            break;  // Next Line
        }
        else  {
            if( $commentMode )  {           // "*/"
                $commentMode = 0;
                printHtml( $matches[1] );
                print $spanComment[1];
                $str = $matches[3];
            }
            else  {
                if( $matches[1] )  printHtml( $matches[1] );
                
                if( $matches[4] )  {        // "//"
                    print $spanComment[0].$matches[4];
                    if( $matches[6] )  printHtml( $matches[6] );
                    print $spanComment[1];
                    break;  // Next Line
                }
                else if( $matches[5] )  {   // "/*"
                    print $spanComment[0].$matches[5];
                    $commentMode = 1;
                    $str = $matches[6];
                }
                else  {                     // Keyword
                    print $spanKeyword[0].$matches[2].$spanKeyword[1];
                    $str = $matches[6];
                }
            }
        }   
}

if( $commentMode )  print $spanComment[1];
fclose( $fp );

print "</pre>";
?>


иногда прочитать текст – недостаточно, чтоб понять, что же за ним стоит:

помню, наш тогдашний декан доказывал:

не бывает универсального способа определять по виду программы,
зависнет ли она, если ей на вход подать её же текст..
потому что, если бы существовала умеющая это определять программа,
она зависала бы от своего собственного вида.



..